#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

// 定义测试数据结构体
typedef struct {
    int id;
    char name[50];
    float value;
    unsigned char data[256];
} PhyTestData;

// Create PHY data directory
int create_phy_directory(const char *dir_path) {
    // Create directory on Linux systems
    struct stat st = {0};
    if (stat(dir_path, &st) == -1) {
        // Directory doesn't exist, create it
        if (mkdir(dir_path, 0700) == 0) {
            printf("Directory created: %s\n", dir_path);
            return 0;
        } else {
            perror("Failed to create directory");
            return -1;
        }
    } else {
        // Directory already exists
        printf("Directory already exists: %s\n", dir_path);
        return 0;
    }
}

// Write test data to file
int write_phy_test_data(const char *file_path, const PhyTestData *data, int count) {
    FILE *file = fopen(file_path, "wb");
    if (!file) {
        printf("Failed to open file: %s\n", file_path);
        return -1;
    }

    // Write data count
    fwrite(&count, sizeof(int), 1, file);
    
    // Write data array
    fwrite(data, sizeof(PhyTestData), count, file);
    
    fclose(file);
    printf("Successfully wrote %d test data entries to %s\n", count, file_path);
    return 0;
}

// Read test data from file
PhyTestData *read_phy_test_data(const char *file_path, int *count) {
    FILE *file = fopen(file_path, "rb");
    if (!file) {
        printf("Failed to open file: %s\n", file_path);
        *count = 0;
        return NULL;
    }

    // Read data count
    size_t result = fread(count, sizeof(int), 1, file);
    if (result != 1) {
        printf("Failed to read data count\n");
        fclose(file);
        *count = 0;
        return NULL;
    }
    
    // Allocate memory
    PhyTestData *data = (PhyTestData *)malloc(sizeof(PhyTestData) * (*count));
    if (!data) {
        printf("Memory allocation failed\n");
        fclose(file);
        *count = 0;
        return NULL;
    }
    
    // Read data array
    result = fread(data, sizeof(PhyTestData), *count, file);
    if (result != (size_t)(*count)) {
        printf("Incomplete data array read. Expected %d entries, actually read %zu entries\n", *count, result);
        free(data);
        fclose(file);
        *count = 0;
        return NULL;
    }
    
    fclose(file);
    printf("Successfully read %d test data entries from %s\n", *count, file_path);
    return data;
}

// Generate random test data
void generate_random_data(PhyTestData *data, int id) {
    data->id = id;
    sprintf(data->name, "PHY Test Data #%d", id);
    data->value = (float)rand() / RAND_MAX * 100.0f;
    
    // Fill with random byte data
    size_t data_size = sizeof(data->data);
    for (size_t i = 0; i < data_size; i++) {
        data->data[i] = rand() % 256;
    }
}

// Print test data
void print_phy_data(const PhyTestData *data) {
    printf("ID: %d\n", data->id);
    printf("Name: %s\n", data->name);
    printf("Value: %.2f\n", data->value);
    printf("Data Preview: ");
    
    // Print only first 10 bytes of data
    for (int i = 0; i < 10; i++) {
        printf("%02X ", data->data[i]);
    }
    printf("...\n\n");
}

int main() {
    // Use relative paths for better cross-platform compatibility
    const char *dir_path = ".";
    const char *file_path = "./phy_test_data.bin";
    const int data_count = 5;
    
    // Initialize random number generator
    srand((unsigned int)time(NULL));
    
    // Create directory
    if (create_phy_directory(dir_path) != 0) {
        return 1;
    }
    
    // Generate test data
    PhyTestData test_data[data_count];
    for (int i = 0; i < data_count; i++) {
        generate_random_data(&test_data[i], i + 1);
    }
    
    // Display data to be written
    printf("===== Data to be Written =====\n");
    for (int i = 0; i < data_count; i++) {
        print_phy_data(&test_data[i]);
    }
    
    // Write data to file
    if (write_phy_test_data(file_path, test_data, data_count) != 0) {
        return 1;
    }
    
    // Read data
    int read_count = 0;
    PhyTestData *read_data = read_phy_test_data(file_path, &read_count);
    
    if (read_data) {
        // Display read data
        printf("\n===== Read Data =====\n");
        for (int i = 0; i < read_count; i++) {
            print_phy_data(&read_data[i]);
        }
        
        // Free memory
        free(read_data);
    }
    
    printf("Test completed! Press any key to exit...");
    getchar();
    return 0;
}